//处理模型
const {
    successModel,
    failModel
} = require('../model/responseModel')


const {
    register,
    sendEmail,
    hasEmail,
    hasUser,
    validCode,
} = require('../controller/register')

//处理用户相关的路由
const handleRegisterRoute =  (req, res) => {
    //定义处理路由的逻辑
    const method = req.method;
    console.log(req.body, req.path);
    switch (method) {

        case 'POST': {
            if (req.path === '/register') {
                //注册
                return register(req.body).then(data => {
                    if (data) {
                        return new successModel('注册成功');
                    } else {
                        return new failModel('注册失败')
                    }
                })
            }else if (req.path === '/register/validCode') {
                //注册
                
                return validCode(req.body).then(data => {
                    if (data) {
                        return new successModel();
                    } else {
                        return new failModel()
                    }
                })
            }
            
            
            else{
                return false;
            }

        };
    case 'GET': {

        //检查邮箱是否已被注册

        if (req.path === '/register/has_email') {
            return hasEmail(req.query).then(data => {
                if (data) {
                    return new successModel(data);
                } else {
                    return new failModel('该邮箱未注册')
                }
            })
        }
        //检查用户名是否已被注册


        else if (req.path === '/register/has_user') {
            return hasUser(req.query).then(data => {
                if (data) {
                    return new successModel(data);
                } else {
                    return new failModel('该用户名未注册')
                }
            })
        }


        //获取邮箱验证码
        else if (req.path === '/register/mailer') {
            
            return  sendEmail(req.query).then(data => {
                if (data) {
                        return new successModel(data)
                } 
                else 
                    return new failModel('发送邮箱验证码失败')
                })
        } 
    };
    default: {
        return false;
    }
    
}
}

module.exports = handleRegisterRoute